using System;
using LockerRack.Helpers;
using LockerRack.Interfaces;
using Primitive.Mvvm.Framework.Implementations;
using Primitive.Mvvm.Framework.Interfaces;
using UIController.Models;

namespace UIController.ViewModels
{
    public class TaskViewModel : BaseViewModel, IModel
    {
        private ToDoModel _model;
        public TaskViewModel(IModel model, IDrawerFile file, IMediator mediator) : base(mediator)
        {
            FileData = file;
            _model = model as ToDoModel;
        }

        public IDrawerFile SelectedFile 
        {
            get { return _model.With(m => m.SelectedFile); }
            set
            {
                if (_model == null)
                    return;

                if (_model.SelectedFile != value)
                {
                    _model.SelectedFile = value;
                    RaisePropertyChanged(this, () => SelectedFile);
                }
            }
        }

        public IDrawerFile FileData { get; private set; }

        public string TaskName
        {
            get
            {
                return FileData != null ? FileData.Name : string.Empty;
            }
            set
            {
                if (FileData == null)
                    return;

                FileData.Name = value;
                RaisePropertyChanged(this, () => TaskName);
            }
        }
        public string TaskFilename
        {
            get
            {
                return FileData != null ? FileData.Filename : string.Empty;
            }
            set
            {
                if (FileData == null)
                    return;

                FileData.Filename = value;
                RaisePropertyChanged(this, () => TaskFilename);
            }
        }
        public Guid TaskId
        {
            get
            {
                return FileData.With(f => f.Contents).Return(c => c.Id, Guid.Empty);
            }
            set
            {
                if (FileData == null || FileData.Contents == null)
                    return;

                FileData.Contents.Id = value;
                RaisePropertyChanged(this, () => TaskId);
            }
        }
        public bool TaskIsCompleted
        {
            get
            {
                var bag = FileData.With(f => f.Contents).Return(c => c.PropertyBag, null);
                
                if (PropertyExists(bag, "IsCompleted"))
                    return bag.IsCompleted;

                return false;
            }
            set
            {
                if (FileData == null || FileData.Contents == null)
                    return;

                if (PropertyExists(FileData.Contents.PropertyBag, "IsCompleted"))
                {
                    FileData.Contents.PropertyBag.IsCompleted = value;
                    RaisePropertyChanged(this, () => TaskIsCompleted);
                }
            }
        }
        
        public DateTime TaskIssueDate
        {
            get
            {
                var bag = FileData.With(f => f.Contents).Return(c => c.PropertyBag, null);

                if (PropertyExists(bag, "IssueDate"))
                    return bag.IssueDate;

                return DateTime.Now;
            }
            set
            {
                if (FileData == null || FileData.Contents == null)
                    return;

                if (PropertyExists(FileData.Contents.PropertyBag, "IssueDate"))
                {
                    FileData.Contents.PropertyBag.IssueDate = value;
                    RaisePropertyChanged(this, () => TaskIssueDate);
                }
            }
        }
        public string TaskDesc
        {
            get
            {
                var bag = FileData.With(f => f.Contents).Return(c => c.PropertyBag, null);

                if (PropertyExists(bag, "Desc"))
                    return bag.Desc;

                return string.Empty;
            }
            set
            {
                if (FileData == null || FileData.Contents == null)
                    return;

                if (PropertyExists(FileData.Contents.PropertyBag, "Desc"))
                {
                    FileData.Contents.PropertyBag.Desc = value;
                    RaisePropertyChanged(this, () => TaskDesc);
                }
            }
        }
        
        public string Path { get; set; }
    }
}